#ifndef CREATEUSERNEW_H
#define CREATEUSERNEW_H

#include <QDialog>
#include <QCheckBox>
#include <QComboBox>
#include <QScrollArea>

#include <QToolButton>
#include <kbubblewidget.h>
using namespace kdk;

#include "fixlabel.h"
#include "qtdbus/uniauthservice.h"
#include "passwdcheckutil.h"

#ifdef ENABLEPQ
extern "C" {

#include <pwquality.h>

}
#define PWCONF "/etc/security/pwquality.conf"
#define RFLAG 0x1
#define CFLAG 0x2
#endif

class QVBoxLayout;
class QHBoxLayout;
class QLabel;
class QRadioButton;
class QFrame;
class QLineEdit;
class QPushButton;
class QButtonGroup;

#define LABELWIDTH 92
#define SECURITY_QUESTION_COUNT 3

class CreateUserNew : public QDialog
{
    Q_OBJECT

public:
    explicit CreateUserNew(QStringList allUsers, QWidget *parent = nullptr);
    ~CreateUserNew();

public:
    void initUI();
    void setConnect();
    void makeSurePwdNeedCheck();
    void nameLegalityCheck(QString username);
    void nameLegalityCheck2(QString nickname);
    void pwdLegalityCheck(QString pwd);
    void initQustionCombox(const QStringList &questionlist);

public:
    QVBoxLayout * m_pScrollVLayout;
    QHBoxLayout * usernameHorLayout;
    QHBoxLayout * nicknameHorLayout;
    QHBoxLayout *hostnameHorLayout;
    QHBoxLayout * newPwdHorLayout;
    QHBoxLayout * surePwdHorLayout;
    QHBoxLayout * tipHorLayout;
    QHBoxLayout * m_pUserTypeHLayout;
    QHBoxLayout * bottomHorLayout;
    QHBoxLayout *usernameTipHorLayout;
    QHBoxLayout *nicknameTipHorLayout;
    QHBoxLayout *hostnameTipHorLayout;
    QHBoxLayout *newPwdTipHorLayout;

    QLabel * usernameLabel;
    QLabel * nicknameLabel;
    QLabel *hostnameLabel;
    QLabel * newPwdLabel;
    QLabel * surePwdLabel;
    QLabel * tipLabel;
    QLabel * m_pUserTypeLabel;
    QComboBox *m_pUserTypeComboBox;
    QPushButton *m_pUserTypeDetailBtn;
    KBubbleWidget *m_pBubbleTips = nullptr;
    QLabel *usernameTipLabel;
    QLabel *nicknameTipLabel;
    QLabel *hostnameTipLabel;
    QLabel *newpwdTipLabel;

    QLineEdit * usernameLineEdit;
    QLineEdit * nicknameLineEdit;
    QLineEdit *hostnameLineEdit;
    QLineEdit * newPwdLineEdit;
    QLineEdit * surePwdLineEdit;

    QPushButton * cancelBtn;
    QPushButton * confirmBtn;

    QCheckBox *m_pSecurityQusetionChecbox;
    FixLabel *m_pSecurityQusetionTitleLabel;
    QVBoxLayout *m_pSecurityQusetionVLayout;
    QWidget *m_pSecurityQuestionWidget;

    QList<QComboBox *> m_questionComboxList;
    QList<QLineEdit *> m_answerLineeditList;

    QStringList m_questionlist;
    QScrollArea *m_pScrollArea;
    QWidget *m_pScrollWidget;

    QLabel *m_pPasswordPromptLabel = nullptr;
    QLineEdit *m_pPasswordPromptLineedit = nullptr;
    QLabel *m_pPasswordPromptTips = nullptr;

public:
    QString userNameTip;
    QString nickNameTip;
    QString newPwdTip;
    QString surePwdTip;
    QString oldName;
    QString oldNickName;

    QStringList _allNames;

    bool enablePwdQuality;
    bool hasNickName = false;

protected:
    bool eventFilter(QObject *watched, QEvent *event);
    void keyPressEvent(QKeyEvent *);
    bool setCunTextDynamic(QLabel * label, QString string);

private:
    bool nameTraverse(QString username);
    bool isHomeUserExists(QString username);
    bool isGroupNameEixsts(QString username);
    bool checkCharLegitimacy(QString password);
    void addSecurityQuestionItem(const int count, bool mutex);
    void setSecurityQuestionVisiable(bool visible);

#ifdef ENABLEPQ
    pwquality_settings_t *settings;
#endif

signals:
    void confirmCreated(QString name, QString fullname, int accounttype, QString pwd);
    void bindSecurityQuestion(QString name, QList<AnswerInfo> answerInfo);
    void bindPasswordTips(const QString &passwordTips);
public Q_SLOTS:
    void setQustionCombox();
    void refreshConfirmBtnStatus();
    void passwordPromptChangedSlot();
};

#endif // CREATEUSERNEW_H
